package MyBlog;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCases extends InitAndEnd{

    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("http://8.130.135.139:8080/blog_content.html","博客正文","自动化测试"));
    }

    /**
     * 博客列表页数量不为0
     */
    @Order(1)
    @Test
    void BlogList() {
        //打开博客列表页
        webDriver.get("http://8.130.135.139:8080/blog_list.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //获取页面上所有博客标题对应的元素
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //如果数量不为0，测试通过
        Assertions.assertNotEquals(0,title_num);
    }
    /**
     * 博客详情页校验
     */
    @Order(2)
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDetail(String expected_url, String expected_title, String expected_blog_title) throws InterruptedException {
        //找到第一篇博客对应的查看全文按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //获取当前页面url
        String cur_url = webDriver.getCurrentUrl();
        sleep(2000);
        //获取当前页面title
        String cur_title = webDriver.getTitle();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //获取博客标题
        String cur_blog_title = webDriver.findElement(By.cssSelector("#title")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(expected_title,cur_title);
        Assertions.assertEquals(expected_blog_title,cur_blog_title);
        if(cur_url.contains(expected_url)) {
            System.out.println("测试通过");
        }else {
            System.out.println(cur_url);
            System.out.println("测试不通过");
        }
    }
    /**
     * 注册新用户
     */
    @Order(3)
    @ParameterizedTest
    @CsvFileSource(resources = "RegSuccess.csv")
    void RegSuccess(String username, String password, String loginUrl, String regUrl) throws InterruptedException {
        //点击登录
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //页面跳转到http://8.130.135.139:8080/login.html
        String cur_url  = webDriver.getCurrentUrl();
        Assertions.assertEquals(loginUrl,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击注册
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //页面跳转到http://8.130.135.139:8080/reg.html
        cur_url  = webDriver.getCurrentUrl();
        Assertions.assertEquals(regUrl,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号zhangsan
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入密码123和确定密码123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#password2")).sendKeys(password);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        //添加显示等待弹框出现
        sleep(2000);
        Alert alert = webDriver.switchTo().alert();
        //操作确定按钮
        alert.accept();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //跳转到登录页面
        cur_url  = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(loginUrl,cur_url);
        sleep(2000);
    }

    /**
     * 输入正确的账号密码，登录成功
     */
    @Order(4)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
      void LoginSuccess(String username, String password, String myblog_listUrl) throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入密码
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击登录按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(2000);
        //跳转到myblog_list
        //获取到当前页面的url
        String cur_url  = webDriver.getCurrentUrl();
        //如果url=http://8.130.135.139:8080/myblog_list.html,测试通过，否则测试不通过
        Assertions.assertEquals(myblog_listUrl,cur_url);
    }
    /**
     * 我的博客列表页校验
     * 新注册的用户应没发表过文章
     */
    @Order(5)
    @Test
    void MyBlogList() {
        //查看页面上是否有“暂无文章，清先添加
        String text = webDriver.findElement(By.cssSelector("#artListDiv > h3")).getText();
        Assertions.assertEquals("暂无文章，请先添加！",text);
    }
    /**
     * 写博客
     */
    @Order(6)
    @Test
    void AddBlog() throws InterruptedException {
        //找到写博客，点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //通过JS将标题进行输入
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
        sleep(2000);
        //点击发布文章
        webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        //添加显示等待弹框出现
        sleep(2000);
        Alert alert = webDriver.switchTo().alert();
        //操作取消按钮
        alert.dismiss();
        //获取当前页面为http://8.130.135.139:8080/myblog_list.html
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://8.130.135.139:8080/myblog_list.html",cur_url);
    }
    /**
     * 效验已发布博客的标题和时间
     */
    @Order(7)
    @Test
    void BlogInfoChecked() {
        //获取第一篇博客的标题
        String first_blog_title = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.title")).getText();
        //获取第一篇博客的发布时间
        String first_blog_time = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.date")).getText();
        //校验博客标题是不是自动化测试
        Assertions.assertEquals("自动化测试",first_blog_title);
        //如果时间时候2023年发布的，测试通过
        if(first_blog_time.contains("2023")) {
            System.out.println("测试通过");
        }else {
            System.out.println("当前时间是"+ first_blog_time);
            System.out.println("测试不通过");
        }

    }
    /**
     * 修改刚才发布的博客
     */
    @Order(8)
    @Test
    void EditBlog() throws InterruptedException {
        //找到写修改，点击
        webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //将标题修改成自动化测试2
        //通过JS将标题进行输入
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试1\"");
        sleep(2000);
        //点击修改文章
        webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        //添加显示等待弹框出现
        sleep(2000);
        Alert alert = webDriver.switchTo().alert();
        //操作确定按钮
        alert.accept();
        //获取当前页面为http://8.130.135.139:8080/myblog_list.html
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://8.130.135.139:8080/myblog_list.html",cur_url);
    }
    /**
     * 效验修改后的博客的标题
     */
    @Order(9)
    @Test
    void EditBlogAfterChecked() {
        //获取第一篇博客的标题
        String first_blog_title = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.title")).getText();
        //校验博客标题是不是自动化测试1
        Assertions.assertEquals("自动化测试1",first_blog_title);

    }

    /**
     * 删除刚才发布修改的文章
     */
    @Order(10)
    @Test
    void DeleteBlog() throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //找到并点击删除按钮
        webDriver.findElement(By.cssSelector("#artListDiv > div > a:nth-child(6)")).click();
        //添加显示等待弹框出现
        sleep(2000);
        Alert alert = webDriver.switchTo().alert();
        //操作确定按钮
        alert.accept();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //查看页面上是否有“暂无文章，清先添加
        String text = webDriver.findElement(By.cssSelector("#artListDiv > h3")).getText();
        Assertions.assertEquals("暂无文章，请先添加！",text);
    }

    /**
     * 注销
     */
    @Order(11)
    @Test
    void Logout() throws InterruptedException {
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        //添加显示等待弹框出现
        sleep(2000);
        Alert alert = webDriver.switchTo().alert();
        //操作确定按钮
        alert.accept();
        //校验url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://8.130.135.139:8080/login.html",cur_url);
        //校验登录按钮
        WebElement webElement = webDriver.findElement(By.cssSelector("#submit"));
        Assertions.assertNotNull(webElement);

    }
}
